package cn.edu.sgu.www.mhxysy.quartz;

import cn.edu.sgu.www.mhxysy.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 清理临时文件的定时任务
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Slf4j
@Component
public class TemporaryFileCleanJob implements Job {

    /**
     * 应用名
     */
    @Value("${spring.application.name}")
    private String name;

    /**
     * 文件上传临时目录
     */
    @Value("${uploads.temp-dir}")
    private String tempDir;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        log.debug("执行TemporaryFileCleanJob定时任务...");

        File directory = new File(tempDir + "/" + name);

        if (directory.isDirectory()) {
            File[] files = directory.listFiles();

            if (files != null && files.length > 0) {
                for (File file : files) {
                    FileUtils.delete(file.getName());
                }
            }
        }

        log.debug("定时任务TempFileCleanJob执行执行完成。");
    }

}